package com.bj.disc.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.ai3d.rfid.R
import com.ai3d.rfid.base.fragment.BaseVmVdbFragment
import com.ai3d.rfid.databinding.FragmentMainNewBinding
import com.bj.disc.base.InitLanguage
import com.bj.disc.service.WebSocketService
import com.bj.disc.ui.main.vm.MainViewModel
import com.bj.disc.widget.TabLayoutMediators
import com.google.android.material.tabs.TabLayout

/**
 * @ProjectName : Kolb
 * @Author : weijinsong
 * @Time : 2023/1/4 16:53
 * @Description : 登录
 *
 * amy在这里是怎么播放的
 * 循环播放双语交替播放Introduction.wav音频吗 你好我叫阿美欢迎来到这个游戏  每隔10S钟
 * 那可不可以点击amy播放不同的声音
 * 我看文件夹里还有其他文件Mainmenu.wav 请选择一个游戏来玩 start1 start2等
 *
 *
 *
 *
 */
class MainFragment : BaseVmVdbFragment<MainViewModel, FragmentMainNewBinding>() {

    val action: Action by lazy { Action() }

    private lateinit var mainAdapter: MainAdapter
    private var fragments = ArrayList<Fragment>()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        initFragment()
        mainAdapter = MainAdapter(this)
        mDatabind.vp2Content.adapter = mainAdapter
        mDatabind.vp2Content.setCurrentItem(0, false)
        mDatabind.vp2Content.isUserInputEnabled = false
        mDatabind.action = action
        TabLayoutMediators(mDatabind.tblNavigation, mDatabind.vp2Content, false) { tab, position ->
            tab.customView = getTabView(position)
        }.attach()
        mDatabind.tblNavigation.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                updateTabTextView(tab, true)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {
                updateTabTextView(tab, false)
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
        mDatabind.tblNavigation.getTabAt(0)?.select()
        val tvTabItem: TextView =
            mDatabind.tblNavigation.getTabAt(0)?.customView!!.findViewById<View>(R.id.tvName) as TextView
        var lavIcon = mDatabind.tblNavigation.getTabAt(0)?.customView!!.findViewById<ImageView>(R.id.lavIcon)
        lavIcon.setBackgroundResource(R.drawable.ami_select)
        tvTabItem.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        tvTabItem.setTextColor(resources.getColor(R.color.color_0788F5))
        startWebSocket()
    }

    fun startWebSocket(){
        var intent= Intent(requireActivity(),WebSocketService::class.java)
        requireActivity().startService(intent)
    }

    private fun updateTabTextView(tabAt: TabLayout.Tab, isSelect: Boolean) {
        val tvTabItem: TextView =
            tabAt.customView!!.findViewById<View>(R.id.tvName) as TextView
        var lavIcon = tabAt.customView!!.findViewById<ImageView>(R.id.lavIcon)
        when (tabAt.position) {
            0 -> {
                if(isSelect) {
                    lavIcon.setBackgroundResource(R.drawable.ami_select)
                }else{
                    lavIcon.setBackgroundResource(R.drawable.ami_normal)
                }
            }
            1 -> {
                if(isSelect) {
                    lavIcon.setBackgroundResource(R.drawable.report_select)
                }else {
                    lavIcon.setBackgroundResource(R.drawable.report_normal)
                }
            }
            2 -> {
                if(isSelect) {
                    lavIcon.setBackgroundResource(R.drawable.set_select)
                }else {
                    lavIcon.setBackgroundResource(R.drawable.set_normal)
                }
            }
            else -> {
                if(isSelect) {
                    lavIcon.setBackgroundResource(R.drawable.ami_select)
                }else{
                    lavIcon.setBackgroundResource(R.drawable.ami_normal)
                }
            }
        }
        if (isSelect) {
            //选中加粗
            tvTabItem.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            tvTabItem.setTextColor(resources.getColor(R.color.color_0788F5))
        } else {
            tvTabItem.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
            tvTabItem.setTextColor(resources.getColor(R.color.color_2D5678))
        }

    }

    private fun getTabView(position: Int): View? {
        val view: View = LayoutInflater.from(activity).inflate(R.layout.item_tab_main, null)
        var lavIcon = view.findViewById<ImageView>(R.id.lavIcon)
        val tvTabItem = view.findViewById<TextView>(R.id.tvName)

        when (position) {
            0 -> {
                lavIcon.setBackgroundResource(R.drawable.ami_normal)
                tvTabItem.setText(R.string.message)
            }
            1 -> {
                lavIcon.setBackgroundResource(R.drawable.report_normal)
                tvTabItem.setText(R.string.report)
            }
            2 -> {
                lavIcon.setBackgroundResource(R.drawable.set_normal)
                tvTabItem.setText(R.string.mine)
            }
            else -> {
                lavIcon.setBackgroundResource(R.drawable.ami_normal)
                tvTabItem.setText(R.string.message)
            }
        }
        return view
    }

    override fun initData() {
        super.initData()

    }

    override fun onPause() {
        super.onPause()
    }

    @SuppressLint("SetTextI18n")
    override fun createObserver() {
        super.createObserver()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
    }


    inner class MainAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
        override fun getItemCount(): Int {
            return fragments.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragments[position]
        }

    }

    private fun initFragment() {
        fragments.clear()
        fragments.add(MessageFragment())
        fragments.add(ReportFragment())
        fragments.add(SetFragment())
    }

    inner class Action {
        fun goBack() {
//            requireActivity().finish()
            InitLanguage.finishAll()
        }

        fun intentToGame(gameName: Int) {
           

        }

        fun logout() {
            //LogoutDialog(requireActivity()).show()
        }

        fun gotoUserReport() {
           // UserReportActivity.startUserReportActivity(requireActivity())
        }
    }
}